package com.efounder.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.addressselector.BottomDialog;
import com.efounder.addressselector.OnAddressSelectedListener;
import com.efounder.addressselector.model.City;
import com.efounder.addressselector.model.County;
import com.efounder.addressselector.model.Province;
import com.efounder.addressselector.model.Street;
import com.efounder.bean.Address;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.http.EFHttpRequest;
import com.efounder.mobilemall.R;
import com.efounder.util.AbStrUtil;
import com.efounder.util.ToastUtil;
import com.efounder.utils.MallRequestUtil;
import com.efounder.utils.ResStringUtil;

import java.util.HashMap;
import java.util.List;

import static com.efounder.utils.MallRequestUtil.ADDRESS_UPDATE_URL;

/**
 * @deprecated
 * Created by will on 18-2-9.
 */

public class MallEditAddressActivity extends Activity implements View.OnClickListener{

    private String TAG = this.getClass().getSimpleName();

    private int position;
    private List<Address> addresses;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_add_address);
        Bundle bundle = getIntent().getExtras();
        addresses = (List<Address>)bundle.get("address");
        position = bundle.getInt("position");
        initView();
    }

    public void initView() {
        View transparentView = (View) findViewById(R.id.transparent_view);
        transparentView.setOnClickListener(this);
        ImageView ivBack = (ImageView) findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        final EditText etName = (EditText) findViewById(R.id.et_address_name);
        etName.setHint(addresses.get(position).getUserName());
        final EditText etPhone = (EditText) findViewById(R.id.et_address_phone);
        etPhone.setHint(addresses.get(position).getPhone());
        final EditText etDistrict = (EditText) findViewById(R.id.et_address_district);
        etDistrict.setHint(addresses.get(position).getArea());
        final EditText etSpecific = (EditText) findViewById(R.id.et_address_specific);
        etSpecific.setHint(addresses.get(position).getAddress());
        TextView tvSave = (TextView) findViewById(R.id.tv_save_address);
        final CheckBox defaultAddressCheck = (CheckBox) findViewById(R.id.cb_default_address);
        final Boolean isCheck = addresses.get(position).getIsSelected().equals("1");
        defaultAddressCheck.setChecked(isCheck);
        defaultAddressCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

            }
        });
        etDistrict.setInputType(InputType.TYPE_NULL);
        etDistrict.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final BottomDialog dialog = new BottomDialog(MallEditAddressActivity.this);
                dialog.setOnAddressSelectedListener(new OnAddressSelectedListener() {
                    @Override
                    public void onAddressSelected(Province province, City city, County county, Street street) {
                        etDistrict.setText(province.name + city.name + county.name);
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }
        });
        tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HashMap<String, String> map = new HashMap<>();
                if (!etPhone.getText().toString().equals("")) {
                    if (!AbStrUtil.isMobileNo(etPhone.getText().toString())){
                        ToastUtil.showToast(MallEditAddressActivity.this, ResStringUtil.getString(R.string.mall_address_enter_right_phone));
                        return;
                    }else{
                        addresses.get(position).setPhone(etPhone.getText().toString());
                        map.put("phone", etPhone.getText().toString());
                    }
                }else {
                    map.put("phone", addresses.get(position).getPhone());
                }
                if (!etName.getText().toString().equals("")){
                    addresses.get(position).setUserName(etName.getText().toString());
                    map.put("userName", etName.getText().toString());
                }else {
                    map.put("userName", addresses.get(position).getUserName());
                }
                if(!etSpecific.getText().toString().equals("")){
                    addresses.get(position).setAddress(etSpecific.getText().toString());
                    map.put("address", etSpecific.getText().toString());
                }else {
                    map.put("address", addresses.get(position).getAddress());
                }
                if (!etDistrict.getText().toString().equals("")) {
                    addresses.get(position).setArea(etDistrict.getText().toString());
                    map.put("area", etDistrict.getText().toString());
                }else {
                    map.put("area", addresses.get(position).getArea());
                }
                addresses.get(position).setIsSelected(defaultAddressCheck.isChecked() ? "1" : "0");
                map.put("addressId", addresses.get(position).getAddressId());
                map.put("userId", EnvironmentVariable.getUserName());
                map.put("isSelected", defaultAddressCheck.isChecked() ? "1" : "0");

                MallRequestUtil.getRequest(TAG, ADDRESS_UPDATE_URL, map, new MallRequestUtil.MallRequestCallback() {
                    @Override
                    public void onSuccess(String response) {
                        Log.e("MallCart", response);
                        String addressId = addresses.get(position).getAddressId();
                        Intent intent = new Intent();
                        intent.putExtra("addressId", addressId);
                        setResult(RESULT_OK, intent);
                        finish();
                        finish();
                    }

                    @Override
                    public void onFail(String error) {

                    }
                });
            }
        });
    }
    @Override
    public void finish() {
        super.finish();
        this.overridePendingTransition(R.anim.bottom_dialog_exit,0);
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    protected void onStop() {
        super.onStop();
        EFHttpRequest.cancelRequest(TAG);
    }
}
